package com.shengdong.water.code.config

import androidx.datastore.preferences.core.edit
import com.shengdong.water.code.bean.UserToken
import com.shengdong.water.code.utils.DataStoreUtil
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.runBlocking

object UserConfig {
    // 同步访问的属性
    var token by DataStoreUtil.string("access_token")
    var user by DataStoreUtil.obj<UserToken>("user_info")
    var isLogin by DataStoreUtil.boolean("is_login")

    // 异步访问的 Flow
    val tokenFlow: Flow<String> = DataStoreUtil.stringFlow("access_token")
    val userFlow: Flow<UserToken?> = DataStoreUtil.objFlow("user_info")
    val isLoginFlow: Flow<Boolean> = DataStoreUtil.booleanFlow("is_login")

     private suspend fun clear() {
        DataStoreUtil.dataStore.edit { preferences ->
            preferences.clear()
        }
    }

    /**
     * 同步清除所有数据
     */
    fun clearSync() = runBlocking {
        clear()
    }

}